package com.sample.Threading;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class ThreadingActivity extends Activity implements OnClickListener{
    ProgressBar bar;
    Button button1, button2;
    Thread back;
    Handler handler=new Handler(){
    @Override
    public void handleMessage(Message m){
    Log.d("debug","messdage handled");	
    bar.incrementProgressBy(5);	
    }
    };
    boolean isRunning=true;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bar=(ProgressBar)findViewById(R.id.progressBar1);
        button1=(Button)findViewById(R.id.button1);
        button2=(Button)findViewById(R.id.button2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
    }
    public void onClick(View v){
    	if (v.getId() == button1.getId()){
    	isRunning=true;
    	}
    	if (v.getId() == button2.getId()){
    	isRunning=false;
    	}
    }
    public void OnStart(){
    super.onStart();
    bar.setProgress(0);
    back.start();
    Log.d("debug","Thread Running");	
    back=new Thread(new Runnable(){
    public void run(){
    try{
    for(int i=0;i<20 && isRunning;i++){
    Thread.sleep(1000);
    handler.sendMessage(handler.obtainMessage());
    }	
    }
    catch(Throwable t){
    	
    }
    }	
    });
    isRunning=true;
    }
    public void onStop(){
    super.onStop();
    isRunning=false;
    }
}